package cmd

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
	"github.com/gogf/gf/v2/os/gcron"

	"yyt/internal/controller/hello"
	mysqlController "yyt/internal/controller/mysql"
	mysqlService "yyt/internal/logic/mysql"
)

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			s := g.Server()
			s.Group("/", func(group *ghttp.RouterGroup) {
				group.Middleware(ghttp.MiddlewareHandlerResponse)
				group.Bind(
					hello.NewV1(),
				)
			})
			s.Group("/mysql", func(group *ghttp.RouterGroup) {
				group.Middleware(ghttp.MiddlewareHandlerResponse)
				group.Bind(
					mysqlController.NewMysqlController(),
				)
			})

			_, _ = gcron.Add(ctx, "@monthly", func(ctx context.Context) {
				g.Log().Print(ctx, "Run once a month, midnight, first of month")
				mysqlService.TruncateTable(ctx, "user")
			}, "TruncateTable_user")

			s.Run()
			return nil
		},
	}
)
